#!/usr/bin/python
import os,sys,re

def getfilename(url):
	if re.search('[^/]+$',url)==None: # file name not found
		raise Exception('%s contains no filename.'%url)
	return re.search('[^/]+$',url).group(0)

def getbasename(filename):
	match=re.match(r'^(.+)\.tar\.(gz|bz2)$',filename)
	if match==None:
		raise Exception('Unknown filename format '+filename)
	return match.group(1)

os.chdir('..')
if os.path.exists('urls')==False: raise Exception('"urls" not found.')
if os.path.exists('scripts')==False: raise Exception('"scripts/" not found.')
urls=open('urls','r')
basenamelist=[]
for url in urls:
	url=url.strip()
	if '?' in url: raise Exception('"?" in '+url)
	if len(url)==0: # empty line
		continue
	elif url[0]=='#': # comment line
		continue
	elif url.find('http://')==0 or url.find('ftp://')==0:
		filename=getfilename(url)
		basename=getbasename(filename)
		if os.path.exists( os.path.join('scripts',basename) )==False:
			print 'No script for '+basename
		if os.path.exists( os.path.join('/source',filename) )==False:
			print 'Not downloaded \n'+url
		basenamelist.append(basename)
	else:
		raise Exception('I cannot handle '+url)

for script in os.listdir('scripts'):
	if script=='.svn': # ignore .svn/
		continue
	elif not script in basenamelist:
		print 'No url for script '+script
